package com.yunsc.admin.controller.store;

import com.baomidou.mybatisplus.extension.api.R;
import com.yunsc.common.utils.PageResult;
import com.yunsc.common.utils.Result;
import com.yunsc.common.utils.SecurityUtil;
import com.yunsc.common.validator.ValidationGroups;
import com.yunsc.store.dto.PageDTO;
import com.yunsc.store.dto.QueryPageDTO;
import com.yunsc.store.entity.YunscPageEntity;
import com.yunsc.store.service.YunscPageService;
import com.yunsc.store.vo.PageDetailVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @version: 1.0
 * @Author: adan
 * @description: 商城页面接口
 * @date: 2024-04-25 13:53
 */
@Api(value = "商城页面接口", tags = "商家信息接口")
@Slf4j
@RestController
@RequestMapping("/page")
public class PageController {

    @Resource
    private YunscPageService pageService;

    @ApiOperation(value = "分页查询页面列表", notes = "分页查询页面列表")
    @GetMapping("/list")
    public Result<?> queryPageList(QueryPageDTO params) {
        Integer currentStoreId = SecurityUtil.getCurrentStoreId();
        PageResult<YunscPageEntity> pageResult = pageService.queryPageList(params, currentStoreId);
        return Result.resultPageList(pageResult);
    }

    @ApiOperation(value = "查询页面详情", notes = "查询页面详情")
    @GetMapping("/detail")
    public Result<?> queryPageDetail(@RequestParam("pageId") Integer pageId) {
        YunscPageEntity pageResult = pageService.getPageDetail(pageId);
        Map<String, Object> result = new HashMap<>();
        result.put("detail", pageResult);
        return Result.ok(result);
    }

    @ApiOperation(value = "查询页面默认数据", notes = "查询页面默认数据")
    @GetMapping("/defaultData")
    public Result<?> queryPageDefaultData() {
        Integer currentStoreId = SecurityUtil.getCurrentStoreId();
        PageDetailVO pageDefaultData = pageService.queryPageDefaultData(currentStoreId);
        return Result.ok(pageDefaultData);
    }

    @ApiOperation(value = "编辑页面", notes = "编辑页面")
    @PostMapping("/edit")
    public Result<?> editPage(@RequestBody @Validated({ValidationGroups.Update.class}) PageDTO params) {
        pageService.editPage(params);
        return Result.ok();
    }

    @ApiOperation(value = "新增页面", notes = "新增页面")
    @PostMapping("/add")
    public Result<?> addPage(@RequestBody PageDTO params) {
        Integer currentStoreId = SecurityUtil.getCurrentStoreId();
        pageService.addPage(params, currentStoreId);
        return Result.ok();
    }

    @ApiOperation(value = "删除页面", notes = "删除页面")
    @PostMapping("/delete")
    public Result<?> deletePage(@RequestBody @Validated({ValidationGroups.Delete.class}) PageDTO params) {
        pageService.deletePage(params.getPageId());
        return Result.ok();
    }
    @ApiOperation(value = "设置首页", notes = "设置首页")
    @PostMapping("/setHome")
    public Result<?> setHomePage(@RequestBody @Validated({ValidationGroups.Update.class}) PageDTO params) {
        pageService.setHomePage(params.getPageId());
        return Result.ok();
    }


}
